package com.java2007.zhangyu.restaurant.dao.impl;

import com.java2007.zhangyu.restaurant.dao.OrderDao;
import com.java2007.zhangyu.restaurant.pojo.Order;
import com.java2007.zhangyu.restaurant.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author zy
 * @Version
 * @date 2021/01/29
 */
public class OrderDaoImpl implements OrderDao {
    /**
     * 保存订单数据
     * @param order
     * @return
     * @throws SQLException
     */
    @Override
    public int save(Order order) throws SQLException {
        QueryRunner runner = new QueryRunner();
        String sql = "INSERT INTO `t_order` " +
                " (`o_id`,`t_id`,`total_price`,`o_pay_state`,`o_create_time`,`o_update_time`,`o_create_user`) " +
                " VALUES (?,?,?,?,?,?,?)";
        return runner.update(JdbcUtils.getConnection(), sql, order.getoId(), order.gettId(),
                order.getTotalPrice(),order.getoPayState(), order.getoCreateTime(), order.getoUpdateTime(), order.getoCreateUser());

    }

    @Override
    public int updateOrderStatus(Long oId, int status) throws SQLException {
        QueryRunner runner = new QueryRunner();
        String sql = " UPDATE `t_order` SET `o_pay_state` = ? WHERE o_id = ?";
        return runner.update(JdbcUtils.getConnection(), sql, status, oId);

    }

    /**
     * 查询所有订单
     * @return
     * @throws SQLException
     */
    @Override
    public List<Order> queryAllOrder() throws SQLException {
        QueryRunner runner = new QueryRunner();
        String sql = "SELECT `o_id` oId,`t_id` tId,`total_price` totalPrice,`o_pay_state` oPayState," +
                "`o_create_time` oCreateTime,`o_update_time` oUpdateTime,`o_create_user` oCreateUser from t_order";
        return runner.query(JdbcUtils.getConnection(),sql, new BeanListHandler<>(Order.class));
    }
}
